package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.DishDao;
import com.example.entity.Dish;
import com.example.entity.DishDto;
import com.example.entity.DishFlavor;
import com.example.service.DishFloverService;
import com.example.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class DishServiceImpl  extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFloverService dishService;



    @Override
    public void saveWithflavor(HttpServletRequest request, DishDto dishDto) {
        this.save(dishDto);

        //存储dishId
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            //添加创建时间和修改时间
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            //添加创建人和修改人
            Long empId = (Long) request.getSession().getAttribute("employee");
            item.setCreateUser(empId);
            item.setUpdateUser(empId);
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishService.saveBatch(flavors);
    }

    @Override
    public DishDto findId(Long id) {
        //调用本类方法查询该id的Dish数据
        Dish dish = this.getById(id);
        //将Dish数据存入到Dto对象当中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的所有口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        //条件是当查询到的口味表中的菜品id查询到的Dish对象中的id对应时
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        //得到的list集合是Dish对象的所有口味
        List<DishFlavor> list = dishService.list(wrapper);
        //将口味存到dishDto当中
        if (list == null) {
            return null;//当前菜品没有口味
        }
        //封装口味
        dishDto.setFlavors(list);
        //将DishDto数据返回主页
        return dishDto;
    }


    @Transactional  //修改数据中添加事务
    public boolean updateByDish(DishDto dishDto, HttpServletRequest request) {
        //1.调用本类方法添加数据Dish
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据,
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishService.remove(queryWrapper);

        //再修改口味的数据
        //得到传如数据的口味对象集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((c) -> {


            //将口味表当中重新添加该商品id
            Long id = (Long) request.getSession().getAttribute("employee");

            c.setDishId(dishDto.getId());

            return c;
        }).collect(Collectors.toList());
        boolean b = dishService.saveBatch(flavors);
        if (b){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleted(Long[] ids) {
        //转换成集合
        List<Long> longs = Arrays.asList(ids);
        //根据longs得到一个集合对象
        List<Dish> dishes = this.listByIds(longs);
        dishes= dishes.stream().map(c->{
            //将删除状态修改到 1
            c.setIsDeleted(1);
            return c;
        }).collect(Collectors.toList());

        //修改状态
        this.updateBatchById(dishes);

        return true;
    }

    @Override
    public boolean updateByIds(Integer status, Long[] ids) {
        //转换数据类型
        List<Long> longs = Arrays.asList(ids);
        //调用方法查询所有数据
        List<Dish> dishes = this.listByIds(longs);
        //修改所有菜品的数据的状态
        dishes=dishes.stream().map((c)->{
            c.setStatus(status);
            return c;
        }).collect(Collectors.toList());
        boolean byId = this.updateBatchById(dishes);
        if (byId==false){
            return false;
        }
        return true;
    }



}
